package wxcrypto

import (
	"crypto/aes"
	"crypto/cipher"
	"encoding/base64"
	"go.uber.org/zap"
)


//AES加密
func AESBase64Encrypt(origin_data string, key string,iv string)(base64_result string, err error){
	var block cipher.Block
	if block, err = aes.NewCipher([]byte(key)); err != nil{
		zap.L().Info(err.Error())
		return
	}
	encrypt := cipher.NewCBCEncrypter(block, []byte(iv))
	var source []byte = PKCS5Padding([]byte(origin_data), 16)
	var dst []byte = make([]byte, len(source))
	encrypt.CryptBlocks(dst, source)
	base64_result = base64.StdEncoding.EncodeToString(dst)
	return
}

//AES解密
func AESBase64Decrypt(encrypt_data string, key string,iv string)(origin_data string, err error){
	var block cipher.Block
	if block, err = aes.NewCipher([]byte(key)); err != nil{
		zap.L().Info(err.Error())
		return
	}
	encrypt := cipher.NewCBCDecrypter(block, []byte(iv))

	var source [] byte
	if source, err = base64.StdEncoding.DecodeString(encrypt_data);err != nil{
		zap.L().Info(err.Error())
		return
	}
	var dst []byte = make([]byte, len(source))
	encrypt.CryptBlocks(dst, source)
	origin_data =  string(PKCS5UnPadding(dst))
	return
}